package integral

import (
	"context"
	"fmt"
	"github.com/shopspring/decimal"
	"private-domain-overseas-service/admin/business/api/internal/repository/integral"
	"private-domain-overseas-service/pkg/constant"
	"private-domain-overseas-service/pkg/xerr"
	"private-domain-overseas-service/tools/ent/hzintegralgoods"
	"time"

	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type IntegralGoodsDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 积分商城商品详情
func NewIntegralGoodsDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IntegralGoodsDetailLogic {
	return &IntegralGoodsDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *IntegralGoodsDetailLogic) IntegralGoodsDetail(req *types.IntegralGoodsDetailRequest) (resp *types.IntegralGoodsDetailResponse, err error) {
	goodsDao := integral.NewIntegralGoodsDao(l.ctx, l.svcCtx)

	//定义换算单位变量
	baiDecimal := decimal.NewFromInt(100)

	//查询商品
	goodsInfo, err := goodsDao.IntegralGoodsConditionsFirst(hzintegralgoods.IDEQ(int(req.Id)))
	if err != nil {
		return nil, xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("查询商品信息失败: %s", err.Error()))
	}

	//组装返回数据
	resp = &types.IntegralGoodsDetailResponse{
		Id:              int64(goodsInfo.ID),
		GoodsInfo:       goodsInfo.GoodsInfo,
		GoodsImage:      fmt.Sprintf("%s%s", constant.IntegralGoodsImage, goodsInfo.GoodsImage),
		GoodsSmallImage: fmt.Sprintf("%s%s", constant.IntegralGoodsImage, goodsInfo.GoodsSmallImage),
		GoodsPrice:      decimal.NewFromInt(goodsInfo.GoodsPrice).Div(baiDecimal).StringFixed(2),
		Sort:            goodsInfo.GoodsSort,
		Status:          goodsInfo.GoodsStatus,
		CreatedAt:       time.Unix(goodsInfo.CreatedAt, 0).Format(time.DateTime),
		UpdatedAt:       time.Unix(goodsInfo.UpdateAt, 0).Format(time.DateTime),
	}

	return resp, nil
}
